package comp302.prototype.slide;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Iterator;

import javax.swing.JPanel;

import comp302.prototype.MainWindow;
import comp302.prototype.component.Component;
import comp302.prototype.slide.handler.*;
import comp302.prototype.xml.XMLNode;

public class SlideItem extends JPanel {
	private static final long serialVersionUID = -6458051269294137420L;
	
	private SlideEditHandler editHandler;
	private ComponentHandler componentHandler;

	public SlideItem() {
		super();
		this.setFocusable(true);
		
		editHandler = new SlideEditHandler(this);
		this.addMouseListener(editHandler);
		this.addMouseMotionListener(editHandler);
		this.addKeyListener(editHandler);
		
		componentHandler = new ComponentHandler();
	}

	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, getWidth(), getHeight());
		Iterator<Component> iterator = componentHandler.getIterator();
		while (iterator.hasNext()) {
			iterator.next().draw(g);
		}
		editHandler.drawTemp(g);
	}
	
	public SlideEditHandler getEditHandler() {
		return editHandler;
	}
	
	public ComponentHandler getComponentHandler() {
		return componentHandler;
	}

	public XMLNode toXML() {
		XMLNode node = new XMLNode("slide");
		
		node.setAttribute("id", Integer.toString(MainWindow.getSlideController().getSlideIndex(this)));
		componentHandler.addComponentsToXML(node);
		return node;
	}
	
	public String toString() {
		return toXML().toString();
	}

	public void fromXML(XMLNode node) {
		componentHandler.addComponentsFromXML(node);
	}
}
